package Barang;

import Transaksi.TransaksiForm;
import KategoriBarang.Kategori;
import KategoriBarang.KategoriKontrol;
import Koneksi.Koneksi;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author  Astri
 */
public class BarangForm extends javax.swing.JFrame {

    /** Creates new form BarangForm */
    public BarangForm() {
        try {
            initComponents();

            List<Kategori> kategoris = KategoriKontrol.getKoneksiKontrol().getAllKategori();
            for (Kategori k : kategoris) {
                jenisCombo.addItem(" " + k.getNamaKategori());
            }
            List<Barang> brg = BarangKontrol.getKoneksiKontrol().getAllBarang();
            BarangTableModel model = new BarangTableModel(brg);
            barangTable.setModel(model);
            dataKosong();

        } catch (Exception e) {
            Logger.getLogger(BarangForm.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        menuBar1 = new java.awt.MenuBar();
        menu1 = new java.awt.Menu();
        menu2 = new java.awt.Menu();
        jScrollBar1 = new javax.swing.JScrollBar();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        tambahButton = new javax.swing.JButton();
        ubahButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();
        keluarButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        kodeLabel = new javax.swing.JLabel();
        namaLabel = new javax.swing.JLabel();
        jenisKategoriLabel = new javax.swing.JLabel();
        jualLabel = new javax.swing.JLabel();
        beliLabel = new javax.swing.JLabel();
        stockawalLabel = new javax.swing.JLabel();
        kodeField = new javax.swing.JTextField();
        namaField = new javax.swing.JTextField();
        jenisCombo = new javax.swing.JComboBox();
        beliField = new javax.swing.JTextField();
        jualField = new javax.swing.JTextField();
        stockAwalField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        barangTable = new javax.swing.JTable();
        daftarLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        kembaliButton = new javax.swing.JButton();
        cetakButton = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        menu1.setLabel("File");
        menu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu1ActionPerformed(evt);
            }
        });
        menuBar1.add(menu1);

        menu2.setLabel("Edit");
        menuBar1.add(menu2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setPreferredSize(new java.awt.Dimension(510, 510));

        jLabel1.setFont(new java.awt.Font("Lucida Console", 1, 36));
        jLabel1.setText("FORM BARANG");
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel8.setForeground(new java.awt.Color(153, 0, 0));
        jLabel8.setText("HARI'S MINIMARKET");

        jPanel3.setBackground(new java.awt.Color(204, 204, 255));
        jPanel3.setPreferredSize(new java.awt.Dimension(440, 45));

        tambahButton.setText("Tambah");
        tambahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tambahButtonActionPerformed(evt);
            }
        });

        ubahButton.setText("Ubah");
        ubahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahButtonActionPerformed(evt);
            }
        });

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        keluarButton.setText("Keluar");
        keluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(tambahButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ubahButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(hapusButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(batalButton)
                .addGap(18, 18, 18)
                .addComponent(keluarButton)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ubahButton)
                    .addComponent(hapusButton)
                    .addComponent(batalButton)
                    .addComponent(keluarButton)
                    .addComponent(tambahButton))
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(204, 204, 255));

        kodeLabel.setText("Kode Barang");

        namaLabel.setText("Nama Barang");

        jenisKategoriLabel.setText("Jenis Barang");

        jualLabel.setText("Harga Jual");

        beliLabel.setText("Harga Beli");

        stockawalLabel.setText("Jumlah Stock Awal ");

        kodeField.setCaretColor(new java.awt.Color(153, 0, 0));
        kodeField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                kodeFieldKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                kodeFieldKeyTyped(evt);
            }
        });

        namaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                namaFieldKeyTyped(evt);
            }
        });

        beliField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                beliFieldKeyTyped(evt);
            }
        });

        jualField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jualFieldKeyTyped(evt);
            }
        });

        stockAwalField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                stockAwalFieldKeyTyped(evt);
            }
        });

        jLabel9.setText("Rp");

        jLabel10.setText("Rp");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(stockawalLabel)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(beliLabel)
                                .addGap(116, 116, 116)
                                .addComponent(jLabel10)))
                        .addGap(18, 18, 18))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(namaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                            .addComponent(jenisKategoriLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jualLabel)
                        .addGap(112, 112, 112)
                        .addComponent(jLabel9)
                        .addGap(19, 19, 19))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(kodeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(stockAwalField, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(beliField)
                        .addComponent(jualField)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jenisCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(namaField)
                        .addComponent(kodeField, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kodeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namaLabel)
                    .addComponent(namaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jenisKategoriLabel)
                    .addComponent(jenisCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jualField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jualLabel)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(beliLabel)
                    .addComponent(jLabel10)
                    .addComponent(beliField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stockawalLabel)
                    .addComponent(stockAwalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jualField.getAccessibleContext().setAccessibleName("null\n");

        jSeparator3.setForeground(new java.awt.Color(51, 51, 51));

        barangTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Kode Barang", "Nama Barang", "Jenis Barang", "Harga Jual", "Harga Beli", "Jumlah Stock Awal"
            }
        ));
        barangTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                barangTableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(barangTable);

        daftarLabel.setText("DAFTAR BARANG");

        jLabel2.setText("@copyright2011");

        kembaliButton.setText("Kembali");
        kembaliButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kembaliButtonActionPerformed(evt);
            }
        });

        cetakButton.setText("Cetak");
        cetakButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cetakButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 669, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(556, 556, 556)
                        .addComponent(jLabel12))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(302, 302, 302)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(jLabel8)
                            .addComponent(daftarLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(495, Short.MAX_VALUE)
                .addComponent(kembaliButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cetakButton)
                .addGap(48, 48, 48))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(daftarLabel)
                        .addGap(5, 5, 5)))
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kembaliButton)
                    .addComponent(cetakButton))
                .addGap(192, 192, 192)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-695)/2, (screenSize.height-640)/2, 695, 640);
    }// </editor-fold>//GEN-END:initComponents

    private void ubahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubahButtonActionPerformed
        try {

            String kode = kodeField.getText();
            String nama = namaField.getText();
            String jenis = jenisCombo.getSelectedItem().toString();
            double jual = Double.parseDouble(jualField.getText());
            double beli = Double.parseDouble(beliField.getText());
            double stockAwal = Double.parseDouble(stockAwalField.getText());

            Barang barangBaru = new Barang();
            barangBaru.setKodeBarang(kode);
            barangBaru.setNamaBarang(nama);
            barangBaru.setJenisBarang(jenis);
            barangBaru.setHargaJual(jual);
            barangBaru.setHargaBeli(beli);
            barangBaru.setJumlahStockAwal(stockAwal);

            BarangKontrol barangKontrol = BarangKontrol.getKoneksiKontrol();
            barangKontrol.ubahBarang(barangBaru);

            List<Barang> brg = BarangKontrol.getKoneksiKontrol().getAllBarang();
            BarangTableModel model = new BarangTableModel(brg);
            barangTable.setModel(model);
            JOptionPane.showMessageDialog(this, " " + nama + " berhasil diubah");
        } catch (SQLException ex) {
            Logger.getLogger(BarangForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Data Barang gagal diubah");
        }
        dataKosong();

    }//GEN-LAST:event_ubahButtonActionPerformed

    private void menu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menu1ActionPerformed

    private void tambahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tambahButtonActionPerformed
        try {
            String kode = kodeField.getText();
            String nama = namaField.getText();
            String jenis = jenisCombo.getSelectedItem().toString();
            double jual = Double.parseDouble(jualField.getText());
            double beli = Double.parseDouble(beliField.getText());
            double stockAwal = Double.parseDouble(stockAwalField.getText());
            Barang barangBaru = new Barang();
            barangBaru.setKodeBarang(kode);
            barangBaru.setNamaBarang(nama);
            barangBaru.setJenisBarang(jenis);
            barangBaru.setHargaJual(jual);
            barangBaru.setHargaBeli(beli);
            barangBaru.setJumlahStockAwal(stockAwal);

            BarangKontrol barangKontrol = BarangKontrol.getKoneksiKontrol();
            barangKontrol.tambahBarang(barangBaru);
            List<Barang> brg = BarangKontrol.getKoneksiKontrol().getAllBarang();
            BarangTableModel model = new BarangTableModel(brg);
            barangTable.setModel(model);

            JOptionPane.showMessageDialog(this, " " + nama + " berhasil ditambah");
        } catch (SQLException ex) {
            Logger.getLogger(BarangForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Data barang gagal ditambah.");
        }
        dataKosong();

    }//GEN-LAST:event_tambahButtonActionPerformed

    private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
        try {

            String kode = kodeField.getText();
            String nama = namaField.getText();
            Barang barangBaru = new Barang();
            barangBaru.setKodeBarang(kode);
            barangBaru.setNamaBarang(nama);


            BarangKontrol barangKontrol = BarangKontrol.getKoneksiKontrol();
            barangKontrol.hapusBarang(barangBaru);

            List<Barang> brg = BarangKontrol.getKoneksiKontrol().getAllBarang();
            BarangTableModel model = new BarangTableModel(brg);
            barangTable.setModel(model);

            JOptionPane.showMessageDialog(this, " " + nama + " berhasil dihapus");
        } catch (SQLException ex) {
            Logger.getLogger(BarangForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Data barang gagal dihapus");
        }
        dataKosong();
    }//GEN-LAST:event_hapusButtonActionPerformed

    public void dataKosong() {
        kodeField.setEditable(true);
        namaField.setText("");
        jenisCombo.setSelectedItem(null);
        kodeField.setText("");
        jualField.setText("");
        beliField.setText("");
        stockAwalField.setText("");
    }

    private void keluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_keluarButtonActionPerformed

private void barangTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_barangTableMouseClicked

    int row = barangTable.getSelectedRow();
    String kodegroup = barangTable.getValueAt(row, 0).toString();
    String namagroup = barangTable.getValueAt(row, 1).toString();
    String jenisgroup = barangTable.getValueAt(row, 2).toString();
    double hargajualgroup = Double.parseDouble(barangTable.getValueAt(row, 3).toString());
    double hargabeligroup = Double.parseDouble(barangTable.getValueAt(row, 4).toString());
    double stockawalgroup = Double.parseDouble(barangTable.getValueAt(row, 5).toString());

    kodeField.setText(kodegroup);
    namaField.setText(namagroup);
    jenisCombo.setSelectedItem(jenisgroup);
    jualField.setText(barangTable.getValueAt(row, 3).toString());
    beliField.setText(barangTable.getValueAt(row, 4).toString());
    stockAwalField.setText(barangTable.getValueAt(row, 5).toString());


    kodeField.setEditable(false);
    namaField.requestFocus();
}//GEN-LAST:event_barangTableMouseClicked

private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
    dataKosong();
}//GEN-LAST:event_batalButtonActionPerformed

private void jualFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jualFieldKeyTyped
    if (!Character.isDigit(evt.getKeyChar())) {
        evt.consume();

    }
}//GEN-LAST:event_jualFieldKeyTyped

private void beliFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_beliFieldKeyTyped
    if (!Character.isDigit(evt.getKeyChar())) {
        evt.consume();

    }
}//GEN-LAST:event_beliFieldKeyTyped

private void stockAwalFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stockAwalFieldKeyTyped
    if (!Character.isDigit(evt.getKeyChar())) {
        evt.consume();

    }
}//GEN-LAST:event_stockAwalFieldKeyTyped

private void kodeFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeFieldKeyReleased


    try {
        Barang barang = new Barang();
        String kode = kodeField.getText();
        barang.setKodeBarang(kode);

        barang = BarangKontrol.getKoneksiKontrol().cariBarang(barang);
        String nama = barang.getNamaBarang();
        String jenis = barang.getJenisBarang();
        Double hargaJual = barang.getHargaJual();
        Double hargaBeli = barang.getHargaBeli();
        Double jumlahstockawal = barang.getJumlahStockAwal();

        namaField.setText(nama);
        jenisCombo.setSelectedItem(jenis);
        jualField.setText(String.valueOf(hargaJual));
        beliField.setText(String.valueOf(hargaBeli));
        stockAwalField.setText(String.valueOf(jumlahstockawal));

    } catch (Exception e) {
        Logger.getLogger(BarangForm.class.getName()).log(Level.SEVERE, null, e);

    }
}//GEN-LAST:event_kodeFieldKeyReleased

private void namaFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaFieldKeyTyped
    if (kodeField.getText().isEmpty()) {
        JOptionPane.showMessageDialog(null, "Kode Barang harus dimasukkan terlebih dahulu");
        kodeField.requestFocus();
    }
//    if (Character.isDigit(evt.getKeyChar())) {
//        evt.consume();
//        JOptionPane.showMessageDialog(null, "Nama barang harus berupa huruf");
//    }

}//GEN-LAST:event_namaFieldKeyTyped

private void kodeFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeFieldKeyTyped
    if (evt.getSource().equals(kodeField)) {
        if (kodeField.getText().length() > 3) {
            evt.setKeyChar('\n');

            JOptionPane.showMessageDialog(null, "Jumlah karakter Kode Barang maksimal 4");

        }
    }
    if (!Character.isDigit(evt.getKeyChar())) {
        evt.consume();
        JOptionPane.showMessageDialog(null, "Kode harus berupa angka");
    }

}//GEN-LAST:event_kodeFieldKeyTyped

private void kembaliButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kembaliButtonActionPerformed
    // TODO add your handling code here:
    new TransaksiForm().setVisible(true);
            this.setVisible(false);
}//GEN-LAST:event_kembaliButtonActionPerformed

private void cetakButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cetakButtonActionPerformed
    // TODO add your handling code here:
    Connection conn = null;
    try{
        conn = new Koneksi().getConnection();
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
    
    String reportSource = "./Report/LaporanTransaksi2.jasper";
    Map<String, Object> m = new HashMap<String, Object>();
    try{
        JasperPrint jp = JasperFillManager.fillReport(reportSource, m, conn);
        JasperViewer.viewReport(jp, false);
    }
    catch (JRException ex){
        ex.printStackTrace();
    }
}//GEN-LAST:event_cetakButtonActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BarangForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable barangTable;
    private javax.swing.JButton batalButton;
    private javax.swing.JTextField beliField;
    private javax.swing.JLabel beliLabel;
    private javax.swing.JButton cetakButton;
    private javax.swing.JLabel daftarLabel;
    private javax.swing.JButton hapusButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JComboBox jenisCombo;
    private javax.swing.JLabel jenisKategoriLabel;
    private javax.swing.JTextField jualField;
    private javax.swing.JLabel jualLabel;
    private javax.swing.JButton keluarButton;
    private javax.swing.JButton kembaliButton;
    private javax.swing.JTextField kodeField;
    private javax.swing.JLabel kodeLabel;
    private java.awt.Menu menu1;
    private java.awt.Menu menu2;
    private java.awt.MenuBar menuBar1;
    private javax.swing.JTextField namaField;
    private javax.swing.JLabel namaLabel;
    private javax.swing.JTextField stockAwalField;
    private javax.swing.JLabel stockawalLabel;
    private javax.swing.JButton tambahButton;
    private javax.swing.JButton ubahButton;
    // End of variables declaration//GEN-END:variables
}
